import Dep from './dep.js'

export class Watcher {
  constructor(vm, key, cb) {
    this.vm = vm;
    this.key = key;
    this.cb = cb;
    // 在这里将观察者本身赋值给全局的target，只有被target标记过的才会进行依赖收集
    Dep.target = this;
    // 访问属性触发getter,添加依赖
    this.vm[this.key];
    // 依赖添加后将target设为空，依赖种植
    Dep.target = null
  }

  update() {
    // console.log('属性更新了');
    this.cb.call(this.vm, this.vm[this.key]);
  }
}

export default Watcher
